package org.xtframe.entity;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xtframe.bean.SystemConfig;
import org.xtframe.dao.DataBaseDao;
import org.xtframe.util.CommonUtil;
import org.xtframe.util.SpringUtil;
import org.xtframe.util.SystemConfigUtil;

public class DataBase {
	
	private final Log logger = LogFactory.getLog(getClass());
	
	private DataBaseDao dataBaseDao;
	private SystemConfig systemConfig;
	
	// jndiName
	private String jndiName;
	// 名称
	private String dbName;
	// 数据库类型
	private String dbType;
	// 适配器
	private String driverClassName;
	// 连接地址
	private String url;
	// 用户名
	private String username;
	// 密码
	private String password;
	// 描述
	private String remark;
	// 数据库对应的所有sql
	private ArrayList<String> sql;
	
	public DataBase(){
		dataBaseDao = (DataBaseDao) SpringUtil.getBean("dataBaseDao");
		systemConfig = SystemConfigUtil.getSystemConfig();
		
		sql = new ArrayList <String>();
	}
	
	public boolean init(){
		try {
			sql.clear();
			String strSql = CommonUtil.createSql(systemConfig.getDataBaseInitSqlSql(), jndiName);
			List<String> result = dataBaseDao.executeQuery(strSql, 1, null);
			if(result == null) return false;
			for(String str : result){
				sql.add(str.trim());
			}
			return true;
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return false;
	}
	
	public boolean isInSQL(String sqlId){
		boolean result = false;
		for(String str : sql){
			if(str.equals(sqlId)){
				result = true;
				break;
			}
		}
		return result;
	}

	public String getJndiName() {
		return jndiName;
	}

	public void setJndiName(String jndiName) {
		this.jndiName = jndiName;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getDbType() {
		return dbType;
	}

	public void setDbType(String dbType) {
		this.dbType = dbType;
	}

	public String getDriverClassName() {
		return driverClassName;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public ArrayList<String> getSql() {
		return sql;
	}

	public void setSql(ArrayList<String> sql) {
		this.sql = sql;
	}
	
}
